package com.woniuxy.service.imp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.woniuxy.mapper.HouseMapper;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.pojo.House;
import com.woniuxy.pojo.PageInfo;
import com.woniuxy.pojo.Student;
import com.woniuxy.pojo.User;
import com.woniuxy.service.HouseService;
import lombok.Data;

@Data
@Service
public class HouseServiceImp implements HouseService {
	@Resource
	private HouseMapper houseMapper;
	@Resource
	private UserMapper userMapper;

	@Override
	@Transactional
	public boolean add(House house, String ownerName, String ownerPhone) {
		// ownerName房东姓名。ownerPhone房东电话
		// 新增的房间的已住人数是0
		User user = userMapper.findUserByUser_phone(ownerPhone);
		if (user != null) {
			// 用户已存在，放回其id值
			int user_number = user.getUser_number();
			house.setUser_id(user_number);
			System.out.println("房东已存在，直接返回id:" + user_number);
		} else {
			// 用户不存在，根据信息插入信息,返回id
			User addUser = new User(0, ownerName, ownerPhone, ownerPhone, null);
			int i = userMapper.add(addUser);
			if (i == 0) {
				return false;
			}
			int user_number = userMapper.findUser_number();
			house.setUser_id(user_number);
			System.out.println("房东不存在，插入信息后，返回id:" + user_number);
		}
		house.setOccupy_number(0);
		int i = houseMapper.add(house);
		/*
		 * 根据房东id以及电话插入到用户表并且返回id插入到房间表
		 * 
		 * 先判断房东是否已存在，根据房东姓名和电话查询，已存在直接放回id插入到房间变
		 * 
		 */

		if (i > 0) {
			return true;
		}
		return false;
	}

	@Override
	public PageInfo ShowAllByPage(int pageNumber) {
		PageInfo pageInfo = new PageInfo();
		// 每页显示的数量
		int pageSize = 8;
		// 房间总数
		int count = houseMapper.HouseCount();
		// 计算总页数
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		if (pageNumber > pageCount) {
			// 下一页不能大于总页数
			return null;
		}
		// 取得起始索引
		pageNumber = (pageNumber - 1) * pageSize;
		pageInfo.setPageNumber(pageNumber);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(pageCount);
		List<House> list = houseMapper.ShowAllByPage(pageInfo.getPageNumber(), pageInfo.getPageSize());
		pageInfo.setList(list);
		return pageInfo;
	}

	// int lesseeState是可住房间状态，0是不限，1是可住房间
	// int pageNumber是当前页码
	@Override
	public PageInfo select(House house, int pageNumber, int lesseeState) {
		PageInfo pageInfo = new PageInfo();
		// 每页显示的数量
		int pageSize = 8;
		// 房间总数
		int count = houseMapper.selectCount(house);
		System.out.println("总行数：" + count);
        if (count == 0) {
            return null;
        }
		// 计算总页数
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		if (pageNumber > pageCount) {
			// 下一页不能大于总页数
			return null;
		}
		// 取得起始索引
		pageNumber = (pageNumber - 1) * pageSize;

		pageInfo.setPageNumber(pageNumber);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(pageCount);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("house", house);
		map.put("pageNumber", pageNumber);
		map.put("pageSize", pageSize);
		List<House> list = houseMapper.select(map);

		if (lesseeState != 0) {
			// 移除满员的房间--使用迭代器
			Iterator<House> iterator = list.iterator();
			while (iterator.hasNext()) {
				House house2 = iterator.next();
				if (house2.getOccupy_number() == house2.getMax_number()) {
					iterator.remove();
				}
			}
		}
		pageInfo.setList(list);



		return pageInfo;
	}

	@Override
	public int delete(int house_id) {

		if (findOccupyNumberById(house_id) != 0) {
			// 当前房间还有人住，不能删除
			return 0;
		}
		return houseMapper.delete(house_id);
	}

	@Override
	public int findOccupyNumberById(int house_id) {
		return houseMapper.findOccupyNumberById(house_id);
	}

	// 根据房间id查入住的学生信息
	@Override
	public List<Student> findStudentByHouseId(int house_id) {
		return houseMapper.findStudentByHouseId(house_id);
	}

	@Override
	public List<House> all() {
		return houseMapper.all();
	}

    // 可用房间
	@Override
	public List<House> Available() {
        List<House> list = houseMapper.all();
        // 移除满员的房间--使用迭代器
        Iterator<House> iterator = list.iterator();
        while (iterator.hasNext()) {
            House house2 = iterator.next();
            if (house2.getOccupy_number() == house2.getMax_number()) {
                iterator.remove();
            }
        }
        return list;
	}

	//退房
    @Override
    public int outHouse(int house_id) {
        return houseMapper.outHouse(house_id);
    }



}
